package com.keste.portal.ui.bean;

import com.keste.portal.ui.util.ADFUtils;

import com.keste.portal.ui.util.JSFUtils;

import java.util.ResourceBundle;

import javax.faces.event.ActionEvent;

import oracle.adf.controller.binding.BindingUtils;
import oracle.adf.view.rich.component.rich.RichPopup;
import oracle.adf.view.rich.component.rich.data.RichTable;
import oracle.adf.view.rich.component.rich.layout.RichPanelBox;

import oracle.adf.view.rich.event.DialogEvent;

import oracle.binding.BindingContainer;
import oracle.binding.OperationBinding;
import oracle.adf.model.BindingContext;
import oracle.adf.model.binding.DCBindingContainer;
import oracle.adf.model.binding.DCIteratorBinding;
import oracle.adf.share.logging.ADFLogger;
import oracle.adf.view.rich.context.AdfFacesContext;

import oracle.javatools.resourcebundle.BundleFactory;

import oracle.jbo.ViewObject;
public class BuLookupBean {
    private RichPanelBox editPanelBox;
    private RichTable lookupTableBind;
    private static final String CLASSNAME = BuLookupBean.class.getName();
    ResourceBundle portalBundle =
        BundleFactory.getBundle("com.keste.portal.PortalBundle");
    private static final ADFLogger LOGGER =
        ADFLogger.createADFLogger(CLASSNAME);
    private RichPopup dirtyCheckPopup;

    public BuLookupBean() {
    }

    public String onDeleteAction() {
        BindingContainer bindings = getBindings();
               OperationBinding operationBinding =
                   bindings.getOperationBinding("Delete");
        operationBinding.execute();
        OperationBinding operationBinding1 =
            bindings.getOperationBinding("Commit");
        operationBinding1.execute();    
        DCBindingContainer bindingContainer =
            (DCBindingContainer)BindingUtils.getBindingContext().getCurrentBindingsEntry();
        ViewObject buDepartmentIterator =
            bindingContainer.findIteratorBinding("BuLookupVOIterator").getViewObject();
        buDepartmentIterator.executeQuery();
//        AdfFacesContext.getCurrentInstance().addPartialTarget(lookupTableBind);
//        AdfFacesContext.getCurrentInstance().addPartialTarget(editPanelBox);
        return null;
    }
    public BindingContainer getBindings() {
           return BindingContext.getCurrent().getCurrentBindingsEntry();
       }

    public void setEditPanelBox(RichPanelBox editPanelBox) {
        this.editPanelBox = editPanelBox;
    }

    public RichPanelBox getEditPanelBox() {
        return editPanelBox;
    }

    public void setLookupTableBind(RichTable lookupTableBind) {
        this.lookupTableBind = lookupTableBind;
    }

    public RichTable getLookupTableBind() {
        return lookupTableBind;
    }

    public void setDirtyCheckPopup(RichPopup dirtyCheckPopup) {
        this.dirtyCheckPopup = dirtyCheckPopup;
    }

    public RichPopup getDirtyCheckPopup() {
        return dirtyCheckPopup;
    }

//    public String commitActionListener() {
//        BindingContainer bindings = getBindings();
//        OperationBinding operationBinding = bindings.getOperationBinding("Commit");
//        Object result = operationBinding.execute();
//        if (!operationBinding.getErrors().isEmpty()) {
//            return null;
//        }
//        return null;
//    }

    public void commitActionListener(ActionEvent actionEvent) {
        if (!(Boolean)ADFUtils.evaluateEL("#{controllerContext.currentViewPort.dataDirty}")) {
            JSFUtils.addFacesWarningMessage(portalBundle.getString("THERE_ARE_NO_CHANGES_TO_SAVE"));
           return;
        }
        BindingContainer bindings = getBindings();
        OperationBinding operationBinding =
            bindings.getOperationBinding("Commit");
        operationBinding.execute();
        if (!operationBinding.getErrors().isEmpty()) {
            return;
        }
        JSFUtils.addFacesInformationMessage(portalBundle.getString("ALL_CHANGES_ARE_SAVED"));

       
    }
    
    /**
     * This method is called when cancel button is clicked in Lookup screen.
     * This method rolls back the transaction if data is dirty.
     * @return null
     */
    public void rollbackAction(ActionEvent actionEvent) {
        LOGGER.entering(CLASSNAME, "rollbackAction");
        LOGGER.info(actionEvent.toString());
        try {
            if (!(Boolean)ADFUtils.evaluateEL("#{controllerContext.currentViewPort.dataDirty}")) {
                JSFUtils.addFacesWarningMessage(portalBundle.getString("THERE_ARE_NO_CHANGES_TO_ROLLBACK"));
                LOGGER.exiting(CLASSNAME, "rollbackAction");
                return;
            }
            OperationBinding operationBinding =
                ADFUtils.getOperationBinding("Rollback");
            operationBinding.execute();
            if (!operationBinding.getErrors().isEmpty()) {
                LOGGER.exiting(CLASSNAME, "rollbackAction");
                return;
            }
            JSFUtils.addFacesInformationMessage(portalBundle.getString("ALL_CHANGES_ARE_REVERTED"));
        } catch (Exception e) {
            e.printStackTrace();
            LOGGER.severe(e.getMessage());
            JSFUtils.addFacesErrorMessage(portalBundle.getString("INTERNAL_ERROR_CONTACT_SYSTEM_ADMINISTRATOR"));
        }
        LOGGER.exiting(CLASSNAME, "rollbackAction");
    }

    public void deleteConfirmationDialogListener(DialogEvent dialogEvent) {
        // Add event code here...
        if (dialogEvent.getOutcome() == DialogEvent.Outcome.yes) {
            try {
                DCBindingContainer bindingContainer =
                    (DCBindingContainer)BindingUtils.getBindingContext().getCurrentBindingsEntry();
                
                OperationBinding operationBinding =
                    ADFUtils.getOperationBinding("Delete");
                operationBinding.execute();
                OperationBinding operationBinding1 =
                    ADFUtils.getOperationBinding("Commit");
                operationBinding1.execute();
                ViewObject buLookupVO =
                    bindingContainer.findIteratorBinding("BuLookupVOIterator").getViewObject();
                buLookupVO.executeQuery();
                if (!operationBinding.getErrors().isEmpty()) {
                    LOGGER.exiting(CLASSNAME, "deleteConfirmationDialogListener");
                    return;
                }
                AdfFacesContext.getCurrentInstance().addPartialTarget(getLookupTableBind());
                AdfFacesContext.getCurrentInstance().addPartialTarget(getEditPanelBox());
                JSFUtils.addFacesInformationMessage("Record has been deleted");
            } catch (Exception e) {
                e.printStackTrace();
                LOGGER.severe(e.getMessage());

            }
        }
    }
}
